package com.alipay.util;

import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class AlipayFunction
{
  public static String BuildMysign(Map sArray, String key)
  {
    String prestr = CreateLinkString(sArray);
    prestr = prestr + key;
    String mysign = Md5Encrypt.md5(prestr);
    return mysign;
  }

  public static Map ParaFilter(Map sArray)
  {
    List keys = new ArrayList(sArray.keySet());
    Map sArrayNew = new Hashtable();

    for (int i = 0; i < keys.size(); i++) {
      String key = (String)keys.get(i);
      String value = (String)sArray.get(key);

      if ((value == null) || (value.equals("")) || (key.equalsIgnoreCase("sign")) || (key.equalsIgnoreCase("sign_type")))
      {
        continue;
      }

      sArrayNew.put(key, value);
    }

    return sArrayNew;
  }

  public static String CreateLinkString(Map params)
  {
    List keys = new ArrayList(params.keySet());
    Collections.sort(keys);

    String prestr = "";

    for (int i = 0; i < keys.size(); i++) {
      String key = (String)keys.get(i);
      String value = (String)params.get(key);

      if (i == keys.size() - 1)
        prestr = prestr + key + "=" + value;
      else {
        prestr = prestr + key + "=" + value + "&";
      }
    }

    return prestr;
  }

  public static void LogResult(String sWord)
  {
    try
    {
      FileWriter writer = new FileWriter("D:\\alipay_log" + System.currentTimeMillis() + ".txt");
      writer.write(sWord);
      writer.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static String query_timestamp(String partner)
    throws MalformedURLException, DocumentException, IOException
  {
    String strUrl = "https://mapi.alipay.com/gateway.do?service=query_timestamp&partner=" + partner;
    StringBuffer buf1 = new StringBuffer();
    SAXReader reader = new SAXReader();
    Document doc = reader.read(new URL(strUrl).openStream());

    List<Node> nodeList = doc.selectNodes("//alipay/*");

    for (Node node : nodeList)
    {
      if ((node.getName().equals("is_success")) && (node.getText().equals("T")))
      {
        List<Node> nodeList1 = doc.selectNodes("//response/timestamp/*");
        for (Node node1 : nodeList1) {
          buf1.append(node1.getText());
        }
      }
    }

    return buf1.toString();
  }
}